package com.mfy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mfy.entity.Admin;
import com.mfy.dao.AdminMapper;
import com.mfy.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mfy
 * @since 2023-05-08
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Resource
    private AdminMapper adminMapper;


    @Override
    public Admin login(Admin admin) {
        //1.根据用户名查用户
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",admin.getUsername());
        Admin adminDB = adminMapper.selectOne(wrapper);
        //2.判断用户名是否存在
        if(ObjectUtils.isEmpty(adminDB)){ throw new RuntimeException("用户名错误！");}
        //3.判断密码
        String password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes(StandardCharsets.UTF_8));
        if(!StringUtils.equals(password,adminDB.getPassword())){throw new RuntimeException("密码输入错误！");}
        return adminDB;
    }
}
